package ipad.asia.controller;

import ipad.asia.config.FtpEnterprise;
import ipad.asia.service.enterprise.EnterpriseFeign;
import ipad.asia.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class LoginController {
    @Autowired
    private EnterpriseFeign enterpriseFeign;
   @RequestMapping("/login")
    public String login(@RequestParam(value = "user",required = false)String user,
                        @RequestParam(value = "password",required = false)String password,
                        @RequestParam(value = "inputCode",required = false)String inputCode,
                        @RequestParam(value = "code",required = false)String code,
                        HttpServletRequest request, Model model, HttpServletResponse response) throws IOException {
       HttpSession session=request.getSession();
       session.removeAttribute("warning");
       session.removeAttribute("user");
       session.removeAttribute("shopId");
       if (user == null) {
           model.addAttribute("message","请输入账号");
           return "login";
       } else {
           session.setAttribute("user",user);
           session.setAttribute("shopId",enterpriseFeign.getName(user).getShopId());

           if (this.enterpriseFeign.login(user, MD5Util.inputPassToFormPass(password)) == 4) {
               return "redirect:/index";
           }
           if (this.enterpriseFeign.login(user, MD5Util.inputPassToFormPass(password)) == 3) {
              model.addAttribute("ban","你的账号已经被封禁");
               return "ban";
           }
           if (this.enterpriseFeign.login(user, MD5Util.inputPassToFormPass(password)) == 2) {
               session.setAttribute("warning", "警告！你的账号有严重违规行为，再违规一次就被永久封号");
               return "redirect:/index";
           }
           if (this.enterpriseFeign.login(user, MD5Util.inputPassToFormPass(password)) == 1) {
               System.out.println("你的账号有违规行为");
               session.setAttribute("warning", "警告！你的账号有违规行为，请遵守狗东商城规范");
               return "redirect:/index";
           } else {
               model.addAttribute("message","密码错误");
               return "login";
           }
       }
   }
}
